package com.lagou.kafka.demo.producer;

import org.apache.kafka.clients.producer.*;
import org.apache.kafka.common.serialization.IntegerSerializer;
import org.apache.kafka.common.serialization.StringSerializer;

import java.util.HashMap;
import java.util.Map;

/**
 * @param
 * @author oyz
 * @version 1.0.4
 * @description: TODO
 * @date 2022/8/20 11:34
 */
public class MyProducer {

    public static void main(String[] args) {
        Map<String,Object> config = new HashMap<>();
        config.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG,"192.168.106.130:9092");
        config.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, IntegerSerializer.class);
        config.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
        config.put(ProducerConfig.INTERCEPTOR_CLASSES_CONFIG,"com.lagou.kafka.demo.interceptor.ProducerInterceptor");
        config.put("classContext","oyz is number one");

        ProducerRecord<Integer,String> record = new ProducerRecord<>(
                "tp_demo_01",1001,"oyz is best coder!"
        );
        KafkaProducer<Integer,String> producer = new KafkaProducer<>(config);
        producer.send(record, new Callback() {
            @Override
            public void onCompletion(RecordMetadata metadata, Exception exception) {
                if(exception!=null){
                    throw new RuntimeException("获取kafka异常..."+exception.getMessage());
                }else{
                    System.out.println(metadata.topic());
                    System.out.println(metadata.partition());
                    System.out.println(metadata.offset());
                }
            }
        });
        producer.close();
    }
}
